<?php
include 'db.php';
if(isset($_POST['insertdata'])){
if(@$_POST['filename']){

$parent = @$_POST['parent'];	
require_once 'Excel/reader.php';
$data = new Spreadsheet_Excel_Reader();	
	
	
$data->setOutputEncoding('CP1251');
$data->read($_POST['filename'].'.xls');

error_reporting(E_ALL ^ E_NOTICE);

$data_arr = array();
$field = array();

for ($i = 1; $i <= $data->sheets[0]['numRows']; $i++) {
	for ($j = 1; $j <= $data->sheets[0]['numCols']; $j++) {
		if($i==1){
			$field[] = $data->sheets[0]['cells'][$i][$j];
		}else if($i > 1){
			$strcheck = $data->sheets[0]['cells'][$i][$j];
			if(strlen(trim($strcheck))>0){
				$data_arr[] = $data->sheets[0]['cells'][$i][$j];
			}
		}	
	}
	$cnt = 0;
	if($i > 1){
		$fieldarra = array(	
		"magDesc" => 'MAG Description',
		"phCode" => 'PH Code',
		"itemName" => 'Item Name',
		"itemDesc" => 'Item Description',
		"itemPrice" => '',
		"wattage" => 'Wattage',
		"halogenWatt" => 'Halogen Wattage',
		"socket" => 'Socket',
		"beamInt" => 'Beam Intensity',
		"beamAngle" => 'Beam Angle',
		"lifetime" => 'Lifetime',
		"dimmable" => 'Dimmable',
		"lumenOutput" => 'Lumen Output',
		"efficacy" => 'Efficacy',
		"unitsPerPack" => 'Units Per Pack',
		"colorRendering" => 'Color Rendering Index',
		"colorTemp" => 'Color Temp',
		"NC" => '12NC 929000',
		"isActive" => 1,
		"bulbshape" => 'Bulb Shape',
		"compwattage" => 'Comparable Wattage',
		"exitwindow" => 'Exit window',
		"capbase" => 'Cap base',
		"compatibility" => 'Compatibility',
		"empincluded" => 'EMP050 included',
		"complampwatt" => 'Comparable lamp wattage');
		
		$sql = "INSERT INTO items SET ";
		
		foreach($field as $findex => $fvalue){
			foreach($fieldarra as $farrindex => $fval){
				if($fval == $fvalue){
					if(strlen(trim($data_arr[$cnt])) > 0){
						$sql .= $farrindex." = '".$data_arr[$cnt]."', ";
					}
					$cnt++;
				}
			}
		}
		
		$sql = $sql." isActive = '1'";
		$check = explode("magDesc",$sql);
		if(count($check)>1){
			if(mysql_query($sql)){
				$lastid = mysql_insert_id();
				mysql_query("INSERT INTO cat_items SET cat_id = ".@$_POST['parent'].", item_id = ".$lastid);
			}
		}
		
		unset($data_arr);
	}
}
}
}
//header('location:led-master.php?mngr=prod&subpg=update');
?>
